﻿$(document).ready(function () {
	// change defaults
	if ($.blockUI.defaults) {
		$.blockUI.defaults.message = "Загрузка...";
	}

	// position form-horizontal label
	$(".form-horizontal").resizeFormLabels();

	// change float validation
	if (Globalization && $.validator && $.validator.methods) {
		$.validator.methods.number = function (value, element) {
			return this.optional(element) || value == '' || Globalization.parseFloat(value);
		}
	}
});

$.fn.resizeFormLabels = function () {
	var form = $(this);
	var maxWidth = 0;
	form.find("> div > label").each(function () {
		if ($(this).css("float") == "left") {
			var width = $(this).width();
			if (width > maxWidth)
				maxWidth = width;
		}
	}).each(function () {
		if ($(this).css("float") == "left")
			$(this).css("width", maxWidth);
	});
}

$.fn.serializeObject = function() {
    var o = {};
    var array = this.serializeArray();
    $.each(array, function() {
        if (o[this.name]) {
            if (!o[this.name].push)
                o[this.name] = [o[this.name]];
            o[this.name].push(this.value || '');
        }
		else {
            o[this.name] = this.value || '';
        }
    });
    return o;
};

;  (function ($) {
	$.fn.contextHelp = function () {
		/* Operates on all buttons which match the selector to provide completely automated context help mechanism. */
		$(this).live("click", function () {
			var helpContainer = $(this).parents("[data-help=container]:first");
			var html = "";

			$.each(helpContainer.find("[data-help=itext]"), function () {
				var title = $(this).attr("data-help-title") ? $(this).attr("data-help-title") : $(this).parent().html();
				html += "<p><b>" + title + "</b><br />" + $(this).html() + "</p>";
			});

			$.each(helpContainer.find("[data-help-text]"), function () {
				var title = $(this).attr("data-help-title") ? $(this).attr("data-help-title") : $(this).html();
				html += "<p><b>" + title + "</b><br />" + $(this).attr("data-help-text") + "</p>";
			});

			$.modaldialog.success(html, { title: helpContainer.attr("data-help-title"), width: 700 });

			return false;
		});
	};
})(jQuery);

/* --- Money functions --- */
function toMoney(value, fraction) {
	var f = parseFloat(value);
	var frac = parseInt(fraction);
	if (isNaN(f))
		return "";
	if (isNaN(frac))
		frac = 2;
	else if (frac < 0)
		frac = 0;
	
	var i = 0;
	var str = "";
	while (f >= 1 || i == 0) {
		if (i == 0)
			str = f < 1000 ? f.toFixed(frac) : toFixed2(f, frac, 3);
		else
			str = (f < 1000 ? f.toFixed(0) : toFixed2(f, 0, 3)) + " " + str;
		f = Math.floor(f / 1000);
		i++;
	}

	return str.replace(".", ",");
}

function toFixed2(value, after, before) {
	var max = Math.pow(10, before);
	value = value % max;

	var zeros = "";
	var beforeReal = value.toFixed().length;
	var zeroCount = before - beforeReal;
	while (zeroCount > 0) {
		zeros += "0";
		zeroCount--;
	}

	return zeros + value.toFixed(after);
}

function parseMoney(value) {
	for (var i = value.length - 1; i >= 0; --i) {
		var c = value[i];
		if (c == ' ')
			value = value.substr(0, i) + value.substr(i + 1);
		else if (c == ',')
			value = value.substr(0, i) + '.' + value.substr(i + 1);
	}
	return parseFloat(value);
}